package com.hulk.ratel.web.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.hulk.ratel.common.constant.enums.EC;
import com.hulk.ratel.common.exception.ServiceRteException;
import com.hulk.ratel.common.tool.ValidatorTool;
import com.hulk.ratel.common.constant.Constants;
import com.hulk.ratel.manage.cache.*;
import com.hulk.ratel.manage.inner.DispatchRouteService;
import com.hulk.ratel.manage.commonutil.ContentUtil;
import com.hulk.ratel.manage.service.LogTxnPayRecordService;
import com.hulk.ratel.persistence.entity.*;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2018/8/31 下午10:20
 * @Version: 1.0.0
 * @Modify by:
 * Class description
 */
@Slf4j
public abstract class AbsServiceImpl {

    @Autowired
    protected ContentUtil contentUtil;
    @Autowired
    protected SecurityPartnerCache securityPartnerCache;
    @Autowired
    protected LogTxnPayRecordService logTxnPayRecordService;
    @Autowired
    protected PartnerInfoCache partnerInfoCache;
    @Autowired
    protected PartnerKeyCache partnerKeyCache;
    @Autowired
    protected TxnCodeCache txnCodeCache;
    @Autowired
    protected PayOrgInfoCache payOrgInfoCache;
    @Autowired
    protected DictCodeCache dictCodeCache;
    @Autowired
    protected DictTypeCache dictTypeCache;
    @Autowired
    protected DispatchRouteService dispatchRouteService;




   /* protected void unifyCheck(InitiativeScanPayReqDTO dto, String workId) {
        String txnCode = dto.getHead().geTxncode();
        String partnerNo =dto.getHead().getPartnerNo();
        String traceId =dto.getHead().getTraceId();

        //合作方信息校验
        PartnerInfo partnerInfo = checkPartner(partnerNo, workId);
        //交易权限校验
        Txncode txnCodeInfo = checkTxnCode(txnCode, workId);
        //交易重复性校验
        checkTxnLog(traceId, workId, partnerNo);
    }*/


    @NonNull
    protected String validateBean(Object o, String workId) {
        String validateResult = ValidatorTool.validateBean(o);
        if (!Strings.isNullOrEmpty(validateResult)) {
            throw new ServiceRteException( EC.E_100001.getErrCode(), validateResult);
        }
        return validateResult;
    }

    /**
     * 查询合作方信息
     */
    @NonNull
    protected PartnerInfo checkPartner(String partnerNo) {
        PartnerInfo partner = Optional.ofNullable(partnerInfoCache.findPartnerInfo(partnerNo)).orElseThrow(()->new ServiceRteException( EC.E_100002));
        if (Strings.isNullOrEmpty(partner.getPartnerNo())) {
            throw new ServiceRteException( EC.E_200153.getErrCode(), EC.E_200153.getErrDesc());
        }
        log.info("partnerNo:{}  合作方信息校验通过!", partnerNo);
        return partner;
    }

    /**
     * 交易权限信息
     */
    @NonNull
    protected Txncode checkTxnCode(String txnCode) {
        Txncode paraTxncode =  Optional.ofNullable(txnCodeCache.findTxnCodeInfo( txnCode)).orElseThrow(()->new ServiceRteException( EC.E_100009));
        log.info("txnCode：{}  交易权限校验通过!", txnCode);
        return paraTxncode;
    }

    /**
     * 支付机构查询
     */
    @NonNull
    protected OrgInfo queryPayOrgInfo( String payOrgCode) {
		OrgInfo org =  Optional.ofNullable(payOrgInfoCache.findOrgInfo( payOrgCode)).orElseThrow(()->{ log.error(" 查询支付机构：{} 信息不存在！", payOrgCode);return new ServiceRteException( EC.E_200105);});
        log.info(" 查询支付机构：{} 信息存在！", payOrgCode);
        return org;
    }
    
    /**
     * 支付流水重复校验
     */
    @NonNull
    protected void checkTxnLog(String partnerNo,String tradeNo) {
        LogTxnPayRecord param = new LogTxnPayRecord();
        param.setTraceId(tradeNo);
        param.setPartnerNo(partnerNo);
        Wrapper<LogTxnPayRecord> queryTxnPayRecord = new QueryWrapper<>(param);
        int count = logTxnPayRecordService.count(queryTxnPayRecord);
        if (Constants.ZERO_INT < count) {
            log.error("支付流水重复校验不通过！ tradeNo：{} ", tradeNo);
            throw new ServiceRteException( EC.E_200106.getErrCode(), EC.E_200106.getErrDesc());
        }
        log.info("支付流水重复校验通过！ partnerNo:{} tradeNo:{} ", partnerNo, tradeNo);
    }
    

}
